package com.lei.mingyuemall.product.app;

import java.util.List;
import java.util.Map;


import com.lei.mingyuemall.product.entity.AttrEntity;
import com.lei.mingyuemall.product.service.AttrAttrgroupRelationService;
import com.lei.mingyuemall.product.service.AttrService;
import com.lei.mingyuemall.product.service.CategoryService;
import com.lei.mingyuemall.product.vo.AttrGroupRealtionVo;
import com.lei.mingyuemall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lei.mingyuemall.product.entity.AttrGroupEntity;
import com.lei.mingyuemall.product.service.AttrGroupService;
import com.lei.common.utils.PageUtils;
import com.lei.common.utils.R;



/**
 * 属性分组
 *
 * @author leitao
 * @email leitao@gmail.com
 * @date 2020-09-13 13:20:52
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    AttrService attrService;
    @Autowired
    AttrAttrgroupRelationService relationService;

    /**
     * /product/attrgroup/{catelogId}/withattr
     */

    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        //查处当前分类所有分组
        //查出每个属性分组的所有属性
        List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrsBycatelogId(catelogId);
        return R.ok().put("data",vos);
    }
    /**
     * http://localhost:188/api/product/attrgroup/1/attr/relation?t=1601286415594
     * @param attrgroupId
     * @return
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
       List<AttrEntity> entityList =  attrService.getRellationAttr(attrgroupId);
       return R.ok().put("data",entityList);
    }
    /**
     * http://localhost:188/api/product/attrgroup/2/noattr/relation?t
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params){
        PageUtils page = attrService.getNoRellationAttr(params,attrgroupId);
        return R.ok().put("page",page);
    }
    /**
     * /product/attrgroup/attr/relation
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRealtionVo> vos){
        relationService.saveBatch(vos);
        return R.ok();
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }
    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R listByCateLogId(@RequestParam Map<String, Object> params,@PathVariable Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[] path = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupRealtionVo[] vos){
		attrService.deleteRelation(vos);

        return R.ok();
    }

    /**
     * 删除
     * http://localhost:188/api/product/attrgroup/attr/relation/delete
     */
    @PostMapping("/attr/relation/delete")
    public R delete(@RequestBody AttrGroupRealtionVo[] vos){
        attrService.deleteRelation(vos);
        return R.ok();
    }

}
